package org.jdsm.flow.gxt.users.client.widgets.role;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.RequestBuilder;
import com.sencha.gxt.data.client.loader.HttpProxy;
import com.sencha.gxt.data.shared.Store;
import com.sencha.gxt.data.shared.loader.TreeLoader;
import com.sencha.gxt.data.shared.writer.JsonWriter;
import com.sencha.gxt.widget.core.client.event.*;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.editing.GridEditing;
import com.sencha.gxt.widget.core.client.grid.editing.GridRowEditing;
import com.sencha.gxt.widget.core.client.info.Info;
import org.jdsm.flow.gxt.users.client.UserAutoBeanFactory;

/**
 * Created by IntelliJ IDEA.
 * User: mikom
 * Date: 9/25/12
 * Time: 3:50 PM
 */
public class EditingForRoles {
    Grid<UserAutoBeanFactory.Role> grid;
    GridEditing<UserAutoBeanFactory.Role> editing;
    ColumnConfig<UserAutoBeanFactory.Role,String> columnName;
    ColumnConfig<UserAutoBeanFactory.Role, Integer> columnPermission;

    TreeLoader<UserAutoBeanFactory.Response> loader;
    UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);
    CompleteEditEvent<UserAutoBeanFactory.Role> eventHandler;


    String path = "data/response.json";


    public EditingForRoles(Grid<UserAutoBeanFactory.Role> grid,
                           ColumnConfig<UserAutoBeanFactory.Role,String> columnName,
                           ColumnConfig<UserAutoBeanFactory.Role, Integer> columnPermission, String saveOrUpdatePath) {
        this.grid = grid;
        this.columnName = columnName;
        this.columnPermission = columnPermission;
        this.path = saveOrUpdatePath;
    }

    public void init() {
        editing = new GridRowEditing<UserAutoBeanFactory.Role>(grid);

        editing.addEditor(columnName, new TextField());
        editing.addEditor(columnPermission, new NumberField<Integer>(new NumberPropertyEditor.IntegerPropertyEditor()));


        grid.addCellClickHandler(new CellClickEvent.CellClickHandler() {
            @Override
            public void onCellClick(CellClickEvent event) {

            }
        });

        grid.addCellDoubleClickHandler(new CellDoubleClickEvent.CellDoubleClickHandler() {
            @Override
            public void onCellClick(CellDoubleClickEvent event) {
                editing.setEditableGrid(grid);
            }
        });

        editing.addCompleteEditHandler(new RoleEventHandler());

        UserAutoBeanFactory.ResponseDataRecordJsonReader reader =
                new UserAutoBeanFactory.ResponseDataRecordJsonReader(factory, UserAutoBeanFactory.Response.class);



        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, path);
        HttpProxy<UserAutoBeanFactory.Response> proxy = new HttpProxy<UserAutoBeanFactory.Response>(builder);
        JsonWriter<UserAutoBeanFactory.Response> writer =
                new JsonWriter<UserAutoBeanFactory.Response>(factory, UserAutoBeanFactory.Response.class);
        proxy.setWriter(writer);
        loader = new TreeLoader<UserAutoBeanFactory.Response>(proxy, reader);
    }

    public void commit(UserAutoBeanFactory.Role role){
        grid.getStore().commitChanges();
        UserAutoBeanFactory.RoleResponse config = (UserAutoBeanFactory.RoleResponse) loader.getLastLoadConfig();
        if(config == null){
            config = factory.roleResponseResult().as();
        }

        config.setRole(role);
        loader.load(config);

    }

    public void reject(){
        grid.getStore().rejectChanges();
    }

    class RoleEventHandler implements CompleteEditEvent.CompleteEditHandler<UserAutoBeanFactory.Role> {
        @Override
        public void onCompleteEdit(CompleteEditEvent<UserAutoBeanFactory.Role> roleCompleteEditEvent) {
            UserAutoBeanFactory.Role role = roleCompleteEditEvent.getSource().getEditableGrid().getSelectionModel().getSelectedItem();
            commit(role);

        }
    }

}
